#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "structures.h"
#include "outils.h"
#include "expressions.h"

void run_prog(Un_prog *pprog);

int main (int argc, char const* argv[])
{

        if(argc<=1){
                printf("\n |======================= INTERPRETE BASIC ==========================|");
                printf("\n |                                                                   |");
                printf("\n | Erreur : Il faut donner un fichier en entree                      |");
                printf("\n | Exemple: interprete_basic.exe \"Chemin_du_chier_script_basic\"      |");
                printf("\n |                                                                   |");
                printf("\n |===================================================================|\n");
                system("PAUSE");
                exit(1);
        }
        Un_prog *prog = NULL ;
        prog = lecture_fichier(argv[1]);
        run_prog(prog);
        free(prog);
        return 0;
}

void run_prog(Un_prog *pprog){
        if(pprog->pinst==NULL)
                printf("Aucune instruction \n");
        else{
                printf("\n |======================= INTERPRETE BASIC ==========================|");
                printf("\n |                                                                   |");
                printf("\n | Projet DIC1 - Genie Informatique - 2013/2014                      |");
                printf("\n | Ecole Superieure Polytechnique de Dakar (ESP)                     |");
                printf("\n | Par: IKANE , KENZ , NOREYNI ,  APPO                               |");
                printf("\n |                                                                   |");
                printf("\n |===================================================================|\n\n");
                printf("                      Execution de votre programme: \n");
                printf("                      ---------------------------- \n\n");

                int tmp = 0 , retour ,val ;
                Une_var *pc = NULL;
                Une_inst *i = NULL;
                i = pprog->pinst;
                while(i!=NULL){
                        retour = 1;
                        switch(i->type){
                            case PRINT  :   if(i->pexpr->type == STR )
                                                            printf("%s \n",i->pexpr->str);
                                                    else
                                                            printf("%d \n",evaluer_expr(i->pexpr));
                                                    break;
                            case LET    :   i->pvar->val_var = evaluer_expr(i->pexpr );
                                            break;
                            case INPUT  :   val = scanf("%d",&tmp);
                                            i->pvar->val_var = tmp;
                                            break;
                            case GOTO   :   i = i->branch_lab->pinst ;
                                            retour = 1 - retour ;
                                            break;
                            case IF     :   if(evaluer_expr(i->pexpr) !=0){
                                                i = i->branch_lab->pinst ;
                                                retour = 1 - retour ;
                                                }
                                            break;
                        }
                        if(retour) i= i->suiv;
                }
                 printf("                         ------------------- \n\n");
                 printf("                         Execution termin%ce: \n",130);
                 printf("                         ------------------- \n\n");
                 system("PAUSE");
        }
}
